package com.itheima.service.impl;

import com.alibaba.dubbo.config.annotation.Service;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.itheima.dao.RoleDao;
import com.itheima.entity.PageResult;
import com.itheima.entity.QueryPageBean;
import com.itheima.pojo.Role;
import com.itheima.service.RoleService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service(interfaceClass = RoleService.class)
@Transactional
public class RoleServiceImpl implements RoleService {

    @Autowired
    private RoleDao roleDao;

    @Override
    public List<Role> findAll() {
        return roleDao.findAll();
    }

    @Override
    public List<Integer> findCheckItemIdsByCheckGroupId(Integer id) {
        return roleDao.findCheckItemIdsByCheckGroupId(id);
    }

    //添加
    @Override
    public void add(Role role, Integer[] checkitemIds, Integer[] permissionIds) {
        roleDao.add(role);
        Integer roleId = role.getId();
        if(permissionIds != null && permissionIds.length>0){
            for (Integer permissionId : permissionIds) {
                Map map = new HashMap();
                map.put("roleId",roleId);
                map.put("permissionId",permissionId);
                roleDao.addPermission(map);
            }
        }

        if(checkitemIds != null && checkitemIds.length>0){
            for (Integer checkitemId : checkitemIds) {
                Map map = new HashMap();
                map.put("roleId",roleId);
                map.put("menuId",checkitemId);
                roleDao.addMenu(map);
            }
        }
    }
    //分页
    @Override
    public PageResult findPage(QueryPageBean queryPageBean) {

        Integer currentPage = queryPageBean.getCurrentPage();
        Integer pageSize = queryPageBean.getPageSize();
        String queryString = queryPageBean.getQueryString();

        PageHelper.startPage(currentPage,pageSize);
        Page<Role> page = roleDao.findPage(queryString);
        long total = page.getTotal();
        List<Role> result = page.getResult();

        return new PageResult(total,result);
    }

    @Override
    public Role findById(Integer id) {
        return roleDao.findById(id);
    }

    //编辑
    @Override
    public void edit(Integer[] permissionIds, Integer[] checkitemIds, Role role) {

        roleDao.edit(role);

        //先删除权限表
        roleDao.deletePermissionByRoleId(role.getId());

        if(permissionIds !=null && permissionIds.length>0){
            for (Integer permissionId : permissionIds) {
                Map map = new HashMap();
                map.put("roleId",role.getId());
                map.put("permissionId",permissionId);
                roleDao.addPermission(map);
            }
        }

        //先删除菜单表
        roleDao.deleteMenuByRoleId(role.getId());


        if(checkitemIds !=null && checkitemIds.length>0){
            for (Integer checkitemId : checkitemIds) {
                Map map = new HashMap();
                map.put("roleId",role.getId());
                map.put("menuId",checkitemId);
                roleDao.addMenu(map);
            }
        }
    }

    @Override
    public void delete(Integer id) {
        roleDao.deleteMenuByRoleId(id);
        roleDao.deletePermissionByRoleId(id);
        roleDao.deleteUserByRoleId(id);
        roleDao.delete(id);
    }
}
